package com.lefevre.cms.web.action.help;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lefevre.cms.bean.*;
import com.lefevre.cms.bean.help.HelpType;
import com.lefevre.cms.service.help.HelpTypeService;
import com.lefevre.cms.service.setting.SettingService;
import com.lefevre.cms.utils.JsonUtils;
import com.lefevre.cms.web.action.fileSystem.FileManage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 帮助分类
 *
 * @author Lefevre
 */
@Controller
public class HelpTypeController {

    @Resource
    HelpTypeService helpTypeService;
    @Resource
    SettingService settingService;
    @Resource
    FileManage fileManage;

    @ResponseBody
    @RequestMapping("/control/helpType/list")
    public String list(PageForm pageForm, Long parentId) throws Exception {
        Map<String, Object> returnValue = new HashMap<>();

        LambdaQueryWrapper<HelpType> wrapper = new LambdaQueryWrapper<>();
        //如果所属父类有值
        if (parentId != null && parentId > 0L) {
            //所属父类的ID;(params.size()+1)是为了和下面的条件参数兼容
            wrapper.eq(HelpType::getParentId, parentId);
        } else {
            // 没有父类，可以null
            wrapper.eq(HelpType::getParentId, 0L);
        }
        wrapper.orderByDesc(HelpType::getSort);

        PageView<HelpType> pageView = new PageView<>(settingService.findSystemSetting_cache().getBackstagePageNumber(), pageForm.getPage(), 10);
        int firstindex = (pageForm.getPage() - 1) * pageView.getMaxresult();

        //调用分页算法类
        QueryResult<HelpType> qr = new QueryResult<>();

        Page<HelpType> helpTypePage = helpTypeService.page(new Page<>(firstindex, pageView.getMaxresult()), wrapper);

        qr.setTotalrecord(helpTypePage.getTotal());
        qr.setResultlist(helpTypePage.getRecords());

        pageView.setQueryResult(qr);
        returnValue.put("pageView", pageView);

        if (qr.getResultlist() != null && qr.getResultlist().size() > 0) {
            for (HelpType helpType : pageView.getRecords()) {
                helpType.setImage(fileManage.fileServerAddress() + helpType.getImage());
            }
        }
        //分类导航
        if (parentId != null && parentId > 0L) {
            Map<Long, String> navigation = new LinkedHashMap<>();
            HelpType helpType = helpTypeService.findById(parentId);
            if (helpType != null) {
                List<HelpType> parentHelpTypeList = helpTypeService.findAllParentById(helpType);
                for (HelpType p : parentHelpTypeList) {
                    navigation.put(p.getId(), p.getName());
                }
                navigation.put(helpType.getId(), helpType.getName());
                returnValue.put("navigation", navigation);//分类导航
            }
        }
        return JsonUtils.toJSONString(new RequestResult(ResultCode.SUCCESS, returnValue));
    }
}
